import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken, getUserInfo, setUserInfo } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    userInfo: getUserInfo(),
    name: '',
    avatar: '',
    schoolList: [],
    classList: [],
    courseList: [],
    categoryList: [
      { id: 1, name: '公开课' },
      { id: 2, name: '必修' }
    ]
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  },

  SET_SCHOOL: (state, schoolList) => {
    state.schoolList = schoolList.map(el => {
      return {
        id: el.id,
        name: el.school_name
      }
    })
  },
  SET_CLASS: (state, classList) => {
    state.classList = classList.map(el => {
      return {
        id: el.id,
        name: el.class_name
      }
    })
  },
  SET_COURSE: (state, courseList) => {
    state.courseList = courseList.map(el => {
      return {
        id: el.id,
        name: el.course_name
      }
    })
  },
  SET_CATEGORY: (state, categoryList) => {
    state.categoryList = categoryList
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, type } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password, type }).then(data => {
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        setUserInfo({
          username,
          ...data,
          type
        })
        commit('SET_USERINFO', {
          username,
          ...data,
          type
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(() => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  getSchool({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_SCHOOL', [])
      resolve()
    })
  },
  getClass({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_CLASS', [])
      resolve()
    })
  },
  getCourse({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_COURSE', [])
      resolve()
    })
  },
  getCategories({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_CATEGORY', [])
      resolve()
    })
  },
  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() // must remove  token  first
      resetRouter()
      commit('RESET_STATE')
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

